<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>{{ admin.name }}</span>
      </div>
      <div>
        <div>
          <el-upload
              action="/api/system/admin/userFace"
              :headers="headers"
              :data="admin.id"
              :show-file-list="false"
              :on-success="onSuccess"
             >
            <el-image :src="admin.userFace"
                      style="margin-bottom:15px;height: 100px;width: 100px;border-radius: 50px"></el-image>
          </el-upload>


        </div>
        <div style="display: inline;justify-items: start">
          <div>用户名：
            <el-tag>{{ admin.username }}</el-tag>
          </div>
          <div>电话号码：
            <el-tag>{{ admin.telephone }}</el-tag>
          </div>
          <div>手机号码：
            <el-tag>{{ admin.phone }}</el-tag>
          </div>
          <div>居住地址：
            <el-tag>{{ admin.address }}</el-tag>
          </div>
          <div>用户标签：
            <el-tag type="success" v-for="(r,index) in admin.roles" :key="index">{{ r.nameZh }}</el-tag>
          </div>
        </div>

        <div style="display: flex;justify-content: space-around;margin-top: 10px">
          <el-button type="primary" @click="showUpdateAdmin">修改信息</el-button>
          <el-button type="danger" @click="showUpdatePasswordView" >修改密码</el-button>
        </div>
      </div>
    </el-card>
    <el-dialog
        title="修改员工"
        :visible.sync="dialogVisible"
        width="30%"
    >
      <span class="dialogSpan">
        <table>
          <tr>
            <td>
               <el-tag type="success">用户名字</el-tag>
              <el-input
                  size="small"
                  v-model="admin2.name"
                  style="width: 300px;margin-left: 8px;"
              ></el-input>
            </td>
          </tr>
          <tr>
            <td>
               <el-tag type="success">电话号码</el-tag>
              <el-input
                  size="small"
                  v-model="admin2.telephone"
                  style="width: 300px;margin-left: 8px;"></el-input>
            </td>
          </tr>
          <tr>
            <td>
               <el-tag type="success">手机号码</el-tag>
              <el-input
                  size="small"
                  v-model="admin2.phone"
                  style="width: 300px;margin-left: 8px;"></el-input>
            </td>
          </tr>
          <tr>
            <td>
               <el-tag type="success">居住地址</el-tag>
              <el-input
                  size="small"
                  v-model="admin2.address"
                  style="width: 300px;margin-left: 8px;"></el-input>
            </td>
          </tr>
        </table>

      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateAdminInfo">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
        title="修改密码"
        :visible.sync="passwordDialogVisible"
        width="25%"
    >
      <div>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="旧密码" prop="oldPassword">
            <el-input v-model.number="ruleForm.oldPassword"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pass">
            <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="checkPass">
            <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import store from "../../store";

export default {
  name: "userInfo",
  data() {

    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      headers:{
        Authorization: window.sessionStorage.getItem('tokenStr')
      },
      admin: null,
      admin2: null,
      dialogVisible: false,
      passwordDialogVisible: false,
      ruleForm: {
        pass: '',
        checkPass: '',
        oldPassword: ''
      },
      rules: {
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        oldPassword: [
          { validator: validatePass, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    onSuccess(){
      this.initUser();
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ruleForm.adminId = this.admin.id;
          this.postRequest('/api/system/admin/pass',this.ruleForm).then(resp => {
            if (resp){
              //退出操作
              this.postRequest('/logout');
              window.sessionStorage.removeItem('user');
              window.sessionStorage.removeItem('tokenStr');
              //初始化菜单
              this.$store.commit('initRoutes',[]);
              //跳转
              this.$router.replace('/');
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    showUpdatePasswordView(){
      this.passwordDialogVisible = true;
    },
    updateAdminInfo(){
      this.putRequest('/api/system/admin/',this.admin2).then(resp =>{
        if (resp){
          this.initUser();
          this.dialogVisible = false;
        }
      })
    },
    showUpdateAdmin() {
      this.dialogVisible = true;

    },
    initUser() {
      this.getRequest('/login/info').then(resp => {
        if (resp) {
          this.admin = resp;
          this.admin2 = Object.assign({},this.admin); // 复制
          window.sessionStorage.setItem('user',JSON.stringify(resp));
          this.$store.commit('INIT_CURRENTADMIN',resp);
        }
      })
    }
  },
  mounted() {
    this.initUser();
  }
}
</script>

<style scoped>
.box-card {
  margin-top: 15px;
  margin-left: 30%;
  width: 500px;
}
</style>